
package com.example.demo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.demo.R;
import com.example.demo.fragment.NormalFragment;
import com.example.demo.fragment.ViewPagerFragment;
import com.example.demo.view.DrawerView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private Fragment mContent;

    private Fragment viewPagerFragment, normalFragment;

    private ImageView titleLeftIv, titleRightIv;
    private SlidingMenu mSlidingMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        initFragment(savedInstanceState);
        initSlidingMenu();
        initListener();

    }

    private void initView() {
        titleLeftIv = (ImageView) findViewById(R.id.head_left_iv);
        titleRightIv = (ImageView) findViewById(R.id.head_right_iv);
    }

    private void initFragment(Bundle savedInstanceState) {

        viewPagerFragment = new ViewPagerFragment();
        normalFragment = new NormalFragment();

        if (savedInstanceState != null)
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
        if (mContent == null)
            mContent = viewPagerFragment;

        // set the Above View
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_contain, mContent)
                .commit();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    }

    private void initListener() {
        titleLeftIv.setOnClickListener(this);
        titleRightIv.setOnClickListener(this);
    }

    private void initSlidingMenu() {
        mSlidingMenu = new DrawerView(this).initSlidingMenu();
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mSlidingMenu.isMenuShowing() || mSlidingMenu.isSecondaryMenuShowing()) {
                mSlidingMenu.showContent();
            } else {
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    Toast.makeText(this, "在按一次退出", Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                } else {
                    finish();
                }
            }
            return true;
        }
        // 拦截MENU按钮点击事件，让他无任何操作
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.head_left_iv:
                if (mSlidingMenu.isMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    mSlidingMenu.showMenu();
                }
                break;
            case R.id.head_right_iv:
                if (mSlidingMenu.isSecondaryMenuShowing()) {
                    mSlidingMenu.showContent();
                } else {
                    mSlidingMenu.showSecondaryMenu();
                }
                break;

            default:
                break;
        }
    }

    public void switchContent(int type) {

        switch (type) {
            case 1:
                mContent = viewPagerFragment;
                break;
            case 2:
                mContent = normalFragment;
                break;

            default:
                break;
        }

        Log.e("lmf", ">>>>>>>>>switchContent>>>>>>" + mContent.toString());
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_contain, mContent)
                .commit();
        mSlidingMenu.showContent();
    }
}
